// pages/details/details.js
import request from '../../utils/request'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    good_img: "",
    good_sku: "",
    good_price: "",
    showPage: false,
    good_num: 1,
    good_sizeList: '',
    goodSize_img: '',
    good_name: "",
    showText: "",
    good_id: ''
  },
  pictureview() {
    wx.previewImage({
      current: this.data.good_img,
      urls: [this.data.good_img]
    })
  },
  afterenter() {
    // console.log(this.data.good_sku);
    this.ramChangeInfo()
  },
  changeSize() {
    this.setData({
      showPage: true
    })
  },
  downNum() {
    let newNum = this.data.good_num - 1
    this.setData({
      good_num: newNum
    })
    this.ramChangeInfo()
  },
  addNum() {
    let newNum = this.data.good_num + 1
    this.setData({
      good_num: newNum
    })
    this.ramChangeInfo()
  },
  changeGoodSize(e) {
    let cur = e.currentTarget.dataset
    this.data.good_sku[cur.sortindex].value.forEach(item => {
      item.state = false
    })
    this.data.good_sku[cur.sortindex].value[cur.itemindex].state = true;
    let newList = this.data.good_sku
    this.setData({
      good_sku: newList
    })
    this.ramChangeInfo()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      good_id: options.good_id
    })
    let res = await request("api/goodInfo", {
      good_id: options.good_id
    })
    let temObj = JSON.parse(res.data.info[0].edition).sku_column
    console.log(JSON.parse(res.data.info[0].edition));
    if (JSON.parse(res.data.info[0].edition).sku_column&&JSON.parse(res.data.info[0].edition).sku_column.length>0 && temObj &&temObj.length > 0) {
      console.log("进来了");
      let list = []
      temObj.forEach((item, index) => {
        list.push({
          key: item.key,
          value: []
        })
        item.value.forEach((itemm, indexx) => {
          if (indexx === 0) {
            list[index].value.push({
              title: itemm,
              state: true
            })
          } else {
            list[index].value.push({
              title: itemm,
              state: false
            })
          }
        })
      })
      this.setData({
        good_img: res.data.img,
        good_sku: list,
        good_sizeList: JSON.parse(res.data.info[0].edition).sku_list,
        good_name: res.data.good_name
      })
      this.ramChangeInfo()

    }else{
      this.setData({
        good_img: res.data.img,
        good_name: res.data.good_name,
        good_price:res.data.price,
        good_sku: "",
      })
    }

  },
  // 根据规格改变img和价格
  ramChangeInfo() {
    let Read = []
    let num = this.data.good_num
    if(!this.data.good_sku) return 
    this.data.good_sku.forEach(item => {
      item.value.forEach(itemm => {
        if (itemm.state) {
          Read.push(itemm.title)
        }
      })
    })
    if (this.data.good_sizeList) {
      this.data.good_sizeList.forEach(item => {
        if (JSON.stringify(item.sku) == JSON.stringify(Read)) {
          this.setData({
            goodSize_img: item.img,
            good_price: item.price * num,
            showText: item.sku
          })
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})